package wang.hu.bos.web.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import wang.hu.bos.domain.base.FixedArea;
import wang.hu.bos.service.FixedAreaService;
import wang.hu.crm.domain.Customer;

import com.opensymphony.xwork2.ActionContext;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;

	private String[] customerIds;
	private String fixedareaid;

	public void setFixedareaid(String fixedareaid) {
		this.fixedareaid = fixedareaid;
	}

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	@Action(value = "fixedArea_addOrUpdate", results = { @Result(name = "success", location = "./pages/base/fixed_area.html", type = "redirect") })
	public String addOrUpdate() {
		fixedAreaService.save(model);
		return SUCCESS;
	}

	@Action(value = "fixedArea_findNoLinkedCustomer", results = { @Result(name = "success", type = "json") })
	public String findNoLinkedCustomer() {
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9111/crm_management/services/customerService/nolinkedcustomers")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	@Action(value = "fixedArea_findHasLinkedCustomer", results = { @Result(name = "success", type = "json") })
	public String findHasLinkedCustomer() {

		String fixedareaid = ServletActionContext.getRequest().getParameter(
				"fixedareaid");
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9111/crm_management/services/customerService/haslinkedcustomers/"
						+ fixedareaid).accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	// 关联客户到定区
	@Action(value = "fixedArea_assigncustomerstofixedarea", results = { @Result(name = "success", location = "./pages/base/fixed_area.html", type = "redirect") })
	public String assigncustomerstofixedarea() {
		String customeridstr = StringUtils.join(customerIds, ",");
		WebClient
				.create("http://localhost:9111/crm_management/services/customerService/linkedcustomerstofixedarea?customeridstr="
						+ customeridstr + "&fixedareaid=" + fixedareaid).put(
						null);
		return SUCCESS;
	}

	@Action(value = "fixedArea_pageData", results = { @Result(name = "success", type = "json") })
	public String pageData() {
		Pageable pageable = new PageRequest(page - 1, rows);
		Specification<FixedArea> specification = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class),
							model.getId());
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(
							root.get("company").as(String.class),
							"%" + model.getCompany() + "%");
					list.add(p2);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<FixedArea> pagedata = fixedAreaService.pageData(specification,
				pageable);
		pushPageableToValueStack(pagedata);
		return SUCCESS;
	}

}
